package com.e0box.game.utils
{
	import com.e0box.game.event.LocalDataServerEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.OutputProgressEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	

	[Event(name="saveComplete", type="com.e0box.game.event.LocalDataServerEvent")]
	[Event(name="loadComplete", type="com.e0box.game.event.LocalDataServerEvent")]
	[Event(name="saveProgress", type="com.e0box.game.event.LocalDataServerEvent")]
	[Event(name="loadProgress", type="com.e0box.game.event.LocalDataServerEvent")]
	
	public class LocalDataSaver extends EventDispatcher
	{
		public static const EXTENSION:String = ".lud";
		
		private static var _instance:LocalDataSaver;
		
		public function LocalDataSaver(single:validate)
		{
			if(!single) throw new Error("Please use the static method 'getInstance'.");
			
		}
		
		public function saveUserData(name:String, bytes:ByteArray, ext:String = null):void{
			
			//if(!Tools.isCompressed(bytes)) bytes.compress();
			bytes.compress();
			
			var stream:FileStream = new FileStream();
			var file:File = File.applicationStorageDirectory.resolvePath(name + ext ? ("." + ext) : EXTENSION);
			stream.openAsync(file,FileMode.WRITE);
			stream.writeBytes(bytes);
			
			stream.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, onOutputProgress);
		}
		
		private function onOutputProgress(evt:OutputProgressEvent):void{
			var patchEvt:LocalDataServerEvent
			
			if(!evt.bytesPending){
				patchEvt = new LocalDataServerEvent(LocalDataServerEvent.SAVE_COMPLETE);
				
				evt.target.removeEventListener(OutputProgressEvent.OUTPUT_PROGRESS, onOutputProgress);
				evt.target.close();
			}else{
				
				patchEvt = new LocalDataServerEvent(LocalDataServerEvent.SAVE_PROGRESS);
				patchEvt.pending = evt.bytesPending;
			}
			
			dispatchEvent(patchEvt);
		}
		
		public function loadUserData(name:String, ext:String = null):void{
			
			var stream:FileStream = new FileStream();
			var file:File = File.applicationStorageDirectory.resolvePath(name + ext ? ("." + ext) : EXTENSION);
			if(file.exists){
				stream.openAsync(file,FileMode.READ);
				stream.addEventListener(ProgressEvent.PROGRESS, onProgress);
				stream.addEventListener(Event.COMPLETE, onLoaded);
			}else{
				var patchEvt:LocalDataServerEvent = new LocalDataServerEvent(LocalDataServerEvent.LOAD_COMPLETE);
				//patchEvt.bytes = null;
				
				dispatchEvent(patchEvt);
			}
		}
		
		protected function onLoaded(evt:Event):void
		{
			evt.target.removeEventListener(ProgressEvent.PROGRESS, onProgress);
			evt.target.removeEventListener(Event.COMPLETE, onLoaded);
			
			var result:ByteArray = new ByteArray();
			(evt.target as FileStream).readBytes(result);
			
			//if(Tools.isCompressed(result)) result.uncompress();
			result.uncompress();
			
			var patchEvt:LocalDataServerEvent = new LocalDataServerEvent(LocalDataServerEvent.LOAD_COMPLETE);
			patchEvt.bytes = result;
			
			dispatchEvent(patchEvt);
		}
		
		protected function onProgress(evt:ProgressEvent):void
		{
			var patchEvt:LocalDataServerEvent = new LocalDataServerEvent(LocalDataServerEvent.LOAD_PROGRESS);
			patchEvt.loaded = evt.bytesLoaded;
			patchEvt.total = evt.bytesTotal;
			
			dispatchEvent(patchEvt);
		}
		
		public static function get instance():LocalDataSaver{
			if(!_instance) _instance = new LocalDataSaver(new validate());
			
			return _instance;
		}
		
	}
}

class validate{
	public function validate(){
		
	}
}